//Modle :0.66"
//Driver IC:SSD1315
//Interfaces:IIC

#include <at89x51.h>
//#include <reg52.h>
#include <intrins.h>

#define Uchar unsigned char
#define Uint  unsigned int
#define Ulong unsigned long
#define uchar unsigned char
#define uint  unsigned int

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//  Pin Definition
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#define SCL3	P3_1				// Serial Clock Input
#define SDA3	P3_2				// Serial Data Input

code pict1[]={
//0.66",64x48*,   @64x56
0xFE,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0xFE,
0xFF,0x00,0xFC,0x83,0x01,0x83,0x7E,0x00,0x00,0x80,0x80,0x00,0xFC,0x8B,0x09,0x09,
0xF0,0x00,0x00,0xFC,0x8B,0x09,0x09,0xF0,0x00,0x00,0x07,0x00,0x07,0x00,0x00,0x7C,
0x82,0x01,0x01,0x01,0x01,0x82,0x7C,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0xFF,
0x11,0x11,0x11,0x11,0x00,0x00,0xFF,0x01,0x01,0x01,0x83,0xC2,0x7C,0x00,0x00,0xFF,
0xFF,0x80,0x80,0x81,0x81,0x81,0x80,0x80,0x80,0x81,0x81,0x80,0x80,0x81,0x81,0x81,
0x80,0x80,0x80,0x80,0x81,0x81,0x81,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0x81,0x81,0x81,0x81,0x80,0x80,0x80,0x80,0x81,0x81,0x81,0x81,0x81,0x80,0x81,
0x81,0x81,0x81,0x81,0x80,0x80,0x81,0x81,0x81,0x81,0x81,0x80,0x80,0x80,0x80,0xFF,
0xFF,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x80,0x80,0x80,
0x80,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x80,0x80,0x00,0x00,
0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0xFF,
0xFF,0x00,0x00,0x40,0x7F,0x40,0x00,0x1E,0x61,0x40,0x40,0x21,0x00,0x00,0x42,0x00,
0x00,0x23,0x44,0x44,0x48,0x30,0x00,0x23,0x44,0x44,0x48,0x30,0x00,0x7F,0x40,0x40,
0x40,0x21,0x1E,0x00,0x00,0x41,0x40,0x7F,0x40,0x40,0x00,0x61,0x44,0x44,0x3B,0x00,
0x3F,0x61,0x40,0x61,0x3F,0x00,0x00,0x3E,0x45,0x44,0x44,0x38,0x00,0x00,0x00,0xFF,
0xFF,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0xFF,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

void 	delay_ms(Uint t)
{
	Uchar i,j;
	while(--t)
	{
	i=50;	
	j=100;

		while(--j)
			while(--i);
	}
}
void uDelay(Uchar l)
{
	while(l--);
}


void Delay(Uchar n)
{
Uchar i,j,k;

	for(k=0;k<n;k++)
	{
		for(i=0;i<131;i++)
		{
			for(j=0;j<15;j++)
			{
				uDelay(203);	
			}
		}
	}
}

//****************************************************************************
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//  Read/Write Sequence
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void I2C_3(Uchar mcmd)
{
Uchar length = 8;			// Send Command

	while(length--)
	{
		if(mcmd & 0x80)
		{
			SDA3=1;
		}
		else
		{
			SDA3=0;
		}
//		uDelay(3);
		SCL3=1;
//		uDelay(3);
		SCL3=0;
//		uDelay(3);
		mcmd = mcmd << 1;
	}
}

void I2C_Ack3()
{
	SDA3=1;
//	uDelay(3);
	SCL3=1;
//	uDelay(3);
	SCL3=0;
//	uDelay(3);
}

void I2C_NAck3()
{
	SDA3=0;
//	uDelay(3);
	SCL3=1;
//	uDelay(3);
	SCL3=0;
//	uDelay(3);
}

void I2C_Start3()
{
	SDA3=0;
//	uDelay(3);
	SCL3=1;
//	uDelay(3);
	SCL3=0;
//	uDelay(3);
	I2C_3(0x78);
	I2C_Ack3();
}

void I2C_Stop3()
{
	SCL3=1;
//	uDelay(5);
	SDA3=0;
//	uDelay(5);
	SDA3=1;
//	uDelay(5);
}

void Write_Command3(Uchar Data)
{
	I2C_Start3();
	I2C_3(0x00);
	I2C_Ack3();
	I2C_3(Data);
	I2C_Ack3();
	I2C_Stop3();
}

void Write_Data3(Uchar Data)
{
	I2C_Start3();
	I2C_3(0x40);
	I2C_Ack3();
	I2C_3(Data);
	I2C_Ack3();
	I2C_Stop3();
}

void Column_set(unsigned char column)
          {
		     Write_Command3(0x10|(column>>4));
		     Write_Command3(0x00|(column&0x0f));	 
		  }

void Page_set(unsigned char page)
          {
		    Write_Command3(0xb0+page);
		  }

void clear(void)
{
  unsigned char page,column;
  for(page=0;page<6;page++)        //page loop
	 { 
	   Page_set(page);
	   Column_set(32);	  
	   for(column=0;column<64;column++)	//column loop
		  {
			 Write_Data3(0x00);
		  }
	 }
  delay_ms(1);
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void	LCD_Init3(void)
{
//RES3=0;
//uDelay(2);
//RES3=1;
//internal setting

Write_Command3(0xae);	//--turn off oled panel

Write_Command3(0x00);	//--set low column address
Write_Command3(0x10);	//--set high column address

Write_Command3(0x40);	//--set start line address

Write_Command3(0x20);	//--Set Memory Addressing Mode
Write_Command3(0x02);

Write_Command3(0xb0);	//--set page address

Write_Command3(0x81);	//--set contrast control register
Write_Command3(0xff);

Write_Command3(0xa1);	//--set segment re-map 127 to 0   a0:0 to seg127
Write_Command3(0xc8);	//--set com(N-1)to com0  c0:com0 to com(N-1)

Write_Command3(0xa4);	//--Resume to RAM content display

Write_Command3(0xa6);	//--set normal display

Write_Command3(0xa8);	//--set multiples ratio(1to64)
Write_Command3(0x30);	//--1/64 duty

Write_Command3(0xd3);	//--set display offset
Write_Command3(0x00);	//--not offset

Write_Command3(0xd5);	//--set display clock divide ratio/oscillator frequency
Write_Command3(0x80);	//--set divide ratio

Write_Command3(0xd9);	//--set pre-charge period
Write_Command3(0xf1);

Write_Command3(0xda);	//--set com pins hardware configuration
Write_Command3(0x12);

Write_Command3(0xdb);	//--set vcomh
Write_Command3(0x40);

Write_Command3(0x8d);	//--set chare pump enable/disable
Write_Command3(0x14);	//--set(0x10) disable

clear();
Write_Command3(0xaf);	//--turn on oled panel

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=void 	LCD_Init(void)
}

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void Picture1_display(void)
{
  unsigned char page,column;
  Uint k;
k=0;
  for(page=0;page<7;page++)        //page loop
	 { 
	   Page_set(page);
	   Column_set(32);	  
	   for(column=0;column<64;column++)	//column loop
		  {
			 Write_Data3(pict1[k]);
			 k=k+1;
		  }
	 }
  //Delay(5);
}

void fill(void)
{
  unsigned char page,column;
  for(page=0;page<7;page++)        //page loop
	 { 
	   Page_set(page);
	   Column_set(32);	  
	   for(column=0;column<64;column++)	//column loop
		  {
			 Write_Data3(0xff);
		  }
	 }
  //Delay(5);
}

void disA5()
{
	unsigned char i,j;
	for(j=0;j<7;j++)
	{
	   Page_set(j);
	   Column_set(32);
 		for(i=0;i<64;i++)
		{
			Write_Data3(0xAA);
			Write_Data3(0x55);
		}
	}
}		
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void main()
{

	LCD_Init3();

	while(1)

	{   
		fill();
		delay_ms(8);
		disA5();
		delay_ms(8);
		Picture1_display();
		delay_ms(8);
	}
}
